package cn.com.thinker.security.sdk.shiro.realm;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.stereotype.Component;

//import com.thinker.icare.system.entity.SystemUser;
//import com.thinker.icare.system.service.SystemUserService;
import cn.com.thinker.security.sdk.shiro.token.SsoToken;
import cn.com.thinker.security.sdk.system.entity.SystemUser;
import cn.com.thinker.security.sdk.system.service.SystemUserService;

/**
 * Created by crazyHC on 2017/7/4.
 */
@Component("ssoRealm")
public class SsoRealm extends AuthorizingRealm {
	
	@Resource
	private SystemUserService systemUserService;

    public SsoRealm(){
        setAuthenticationTokenClass(SsoToken.class);
    }


    /**
     * 授权
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    /**
     * 认证
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        SsoToken ssoToken = (SsoToken)token;
        if (ssoToken == null ) {
            return null;
        }
        String principal = (String) ssoToken.getPrincipal();
        if(StringUtils.isBlank(principal)){
            return null;
        }
        // create simple authentication info
        
        SystemUser user = systemUserService.getUserByLoginName(principal);
        	
        List<Object> principals = CollectionUtils.asList(new Principal(user.getId(),user.getLoginName(),user.getName()));
        PrincipalCollection principalCollection = new SimplePrincipalCollection(principals, getName());
        return new SimpleAuthenticationInfo(principalCollection, token.getCredentials());
       
    }
    

    /**
     * 授权用户信息
     */
    public static class Principal implements Serializable {

        private static final long serialVersionUID = 1L;

        private String id;
        
        private String loginName;
        private String password;
        private String name;
        public Principal(String id,String loginName,String name) {
            this.id = id;
            this.loginName = loginName;
            this.name = name;
        }

        public String getId() {
            return id;
        }
		
		public String getLoginName() {
			return loginName;
		}

		public void setLoginName(String loginName) {
			this.loginName = loginName;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
		
		
    }
    
}
